// 
// Table that displays system statistics in the GUI.
// Created by: Evan McCullough
// Created on: 13 April 2011
//

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;

public class StatsTable extends JTable{
  String[][] rowData;
  String[] columnNames;
  int rows, cols;
  Connection dbConnection;
  Statement stmt;
  ResultSet rs;
  String systemDisplayed; // This string identifies the system of the vehicle whose statistics are currently being displayed.
  /** System Displayed String Key
      "O"- Overall {"Date","Fuel Economy","Engine Temp", "RPM", "Tire Pressure", "Transmission Status", "Infotainment Status", "Climate Status", "Communication Status", "Lighting/Electrical Status", "Suspension Status", "Brake Status", "Steering Status"}
      "E"- Engine {"Date","Engine Temp", "RPM","Mileage", "Fuel Economy", "0il Life", "Fuel Volume"}
      "T"- Transmission {"Date", "Transmission Status", "Time in N", "Time in D", "Time in R", "Time in 1", "Time in 2", "Time in 3"}
      "I"- Infotainment {"Date","Infotainment Status", "Connected", "Radio Frequency", "CD Inserted", "iPod Connected"}
      "Cl"- Climate {"Date","Internal Temp", "External Temp", "Fan Speed", "Climate Status"}
      "Co"- Communication {"Date","Phone Connected", "Total Call Time", "Communication Status"}
      "LE"- Lighting & Electrical{"Date","Headlight Status", "Power Outlets Status", "Internal Light Status"}
      "TSB"- Tires, Steering, Brakes("Date","Tire Pressure", "Suspension Status", "Brake Status", "Steering Status", "Brake Fluid Life")
  **/
  
  public StatsTable(int r, int c, String[] colNames, String sys){
    super();
    rowData = new String[r][c];
   
    rows = r;
    cols = c;
    columnNames = colNames;
    systemDisplayed = sys;

    DefaultTableModel model = new DefaultTableModel(rowData,columnNames);
    
    setModel(model);
    setGridColor(Color.black);
    setRowSelectionAllowed(false);
    setCellSelectionEnabled(false);
  }

  public void setData(Connection con, Car currCar){
    System.out.println("systemDisplayed: "+systemDisplayed);
    dbConnection = con;
    try{
      stmt = dbConnection.createStatement();
      rs = stmt.executeQuery("SELECT * FROM STATS WHERE VIN = '" +currCar.vin+"' ORDER BY 'DATE_TIME' ASC"); 
      rs.setFetchDirection(ResultSet.FETCH_REVERSE);
      if(rs.last()){
        for(int i=rows-1; i>=0; i--){
          // "O"- Overall {"Date","Fuel Economy","Engine Temp", "RPM", "Tire Pressure", "Transmission Status", "Infotainment Status", "Climate Status", "Communication Status", "Lighting/Electrical Status", "Suspension Status", "Brake Status", "Steering Status"} 
          if (systemDisplayed.equals("O")){
            System.out.println("Setting overall values.");
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("FUEL_ECONOMY"), i, 1);
            setValueAt(rs.getString("ENGINE_TEMP"), i, 2);
            setValueAt(rs.getString("RPM"), i, 3);
            setValueAt(rs.getString("TIRE_PRESSURE"), i,4);
            setValueAt(rs.getString("TRANS_STATUS"), i, 5);
            setValueAt(rs.getString("INFO_STATUS"), i, 6);
            setValueAt(rs.getString("CLIMATE_STATUS"),i, 7);
            setValueAt(rs.getString("COM_STATUS"),i, 8);
            setValueAt(rs.getString("HEADLIGHT_STATUS"),i, 9);
            setValueAt(rs.getString("SUSP_STATUS"),i, 10);
            setValueAt(rs.getString("BRAKE_STATUS"),i, 11);
            setValueAt(rs.getString("STEERING_STATUS"),i, 12);
          }
          if(systemDisplayed.equals("E")){
            // "E"- Engine {"Date","Engine Temp", "RPM","Mileage", "Fuel Economy", "0il Life", "Fuel Volume"} 
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("ENGINE_TEMP"), i, 1);
            setValueAt(rs.getString("RPM"), i, 2);
            setValueAt(rs.getString("MILEAGE"), i, 3);
            setValueAt(rs.getString("FUEL_ECONOMY"), i, 4);
            setValueAt(rs.getString("OIL_LIFE")+"%", i, 5);
            setValueAt(rs.getString("FUEL_LEVEL")+"%",i, 6);
          }
          else if(systemDisplayed.equals("T")){
            //  "T"- Transmission {"Date", "Transmission Status", "Time in N", "Time in D", "Time in R", "Time in 1", "Time in 2", "Time in 3"}
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("TRANS_STATUS"), i, 1);
            setValueAt(rs.getString("TIME_IN_N")+"%", i, 2);
            setValueAt(rs.getString("TIME_IN_D")+"%", i, 3);
            setValueAt(rs.getString("TIME_IN_R")+"%", i, 4);
            setValueAt(rs.getString("TIME_IN_1")+"%", i, 5);
            setValueAt(rs.getString("TIME_IN_2")+"%",i, 6);
            setValueAt(rs.getString("TIME_IN_3")+"%",i, 7);
          }
          else if(systemDisplayed.equals("I")){ 
            //  "I"- Infotainment {"Date","Infotainment Status", "Connected", "Radio Frequency", "CD Inserted", "iPod Connected"}
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("INFO_STATUS"), i, 1);
            setValueAt(rs.getString("CONNECTED"), i, 2);
            setValueAt(rs.getString("RADIO_FREQ"), i, 3);
            setValueAt(rs.getString("CD"), i, 4);
            setValueAt(rs.getString("IPOD"),i, 5);
          }
          else if(systemDisplayed.equals("Cl")){ 
            //  "Cl"- Climate {"Date","Internal Temp", "External Temp", "Fan Speed", "Climate Status"}
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("INTERNAL_TEMP"), i, 1);
            setValueAt(rs.getString("EXTERNAL_TEMP"), i, 2);
            setValueAt(rs.getString("FAN_SPEED"), i, 3);
            setValueAt(rs.getString("CLIMATE_STATUS"), i, 4);
          }
          else if(systemDisplayed.equals("Co")){   
            //  "Co"- Communication {"Date","Phone Connected", "Total Call Time", "Communication Status"}
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("PHONE_CON"), i, 1);
            setValueAt(rs.getString("CALL_TIME"), i, 2);
            setValueAt(rs.getString("COM_STATUS"), i, 3);
          }
          else if(systemDisplayed.equals("LE")){
            // "LE"- Lighting & Electrical{"Date","Headlight Status", "Power Outlets Status", "Internal Light Status"}
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("HEADLIGHT_STATUS"), i, 1);
            setValueAt(rs.getString("POWER_OUTLETS"), i, 2);
            setValueAt(rs.getString("INTERNAL_LIGHTS"), i, 3);
          }
          else if(systemDisplayed.equals("TSB")){ 
            // "TSB"- Tires, Steering, Brakes("Date","Tire Pressure", "Suspension Status", "Brake Status", "Steering Status", "Brake Fluid Life")
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("TIRE_PRESSURE"), i, 1);
            setValueAt(rs.getString("SUSP_STATUS"), i, 2);
            setValueAt(rs.getString("BRAKE_STATUS"), i, 3);
            setValueAt(rs.getString("STEERING_STATUS"), i, 4);
            setValueAt(rs.getString("BRAKE_FLUID_LIFE"), i, 5);
          }
          rs.previous();
        } 
         
      }
    }catch(SQLException e){
      System.out.println("Table population error: "+e);
    }
    
    validate();
    repaint();
    
  }
  
  public void setOldData(ResultSet rs, AutoBiographyGUI parent){
    boolean found = false;
    try{
      while(rs.next() && found == false){
        if(rs.getString("DATE_TIME").equals(parent.oldRecords.getSelectedItem())){
          found = true;
          int i=0;
          // "O"- Overall {"Date","Fuel Economy","Engine Temp", "RPM", "Tire Pressure", "Transmission Status", "Infotainment Status", "Climate Status", "Communication Status", "Lighting/Electrical Status", "Suspension Status", "Brake Status", "Steering Status"} 
          if (systemDisplayed.equals("O")){
            System.out.println("Setting overall values.");
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("FUEL_ECONOMY"), i, 1);
            setValueAt(rs.getString("ENGINE_TEMP"), i, 2);
            setValueAt(rs.getString("RPM"), i, 3);
            setValueAt(rs.getString("TIRE_PRESSURE"), i,4);
            setValueAt(rs.getString("TRANS_STATUS"), i, 5);
            setValueAt(rs.getString("INFO_STATUS"), i, 6);
            setValueAt(rs.getString("CLIMATE_STATUS"),i, 7);
            setValueAt(rs.getString("COM_STATUS"),i, 8);
            setValueAt(rs.getString("HEADLIGHT_STATUS"),i, 9);
            setValueAt(rs.getString("SUSP_STATUS"),i, 10);
            setValueAt(rs.getString("BRAKE_STATUS"),i, 11);
            setValueAt(rs.getString("STEERING_STATUS"),i, 12);
          }
          if(systemDisplayed.equals("E")){
            // "E"- Engine {"Date","Engine Temp", "RPM","Mileage", "Fuel Economy", "0il Life", "Fuel Volume"} 
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("ENGINE_TEMP"), i, 1);
            setValueAt(rs.getString("RPM"), i, 2);
            setValueAt(rs.getString("MILEAGE"), i, 3);
            setValueAt(rs.getString("FUEL_ECONOMY"), i, 4);
            setValueAt(rs.getString("OIL_LIFE")+"%", i, 5);
            setValueAt(rs.getString("FUEL_LEVEL")+"%",i, 6);
          }
          else if(systemDisplayed.equals("T")){
            //  "T"- Transmission {"Date", "Transmission Status", "Time in N", "Time in D", "Time in R", "Time in 1", "Time in 2", "Time in 3"}
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("TRANS_STATUS"), i, 1);
            setValueAt(rs.getString("TIME_IN_N")+"%", i, 2);
            setValueAt(rs.getString("TIME_IN_D")+"%", i, 3);
            setValueAt(rs.getString("TIME_IN_R")+"%", i, 4);
            setValueAt(rs.getString("TIME_IN_1")+"%", i, 5);
            setValueAt(rs.getString("TIME_IN_2")+"%",i, 6);
            setValueAt(rs.getString("TIME_IN_3")+"%",i, 7);
          }
          else if(systemDisplayed.equals("I")){ 
            //  "I"- Infotainment {"Date","Infotainment Status", "Connected", "Radio Frequency", "CD Inserted", "iPod Connected"}
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("INFO_STATUS"), i, 1);
            setValueAt(rs.getString("CONNECTED"), i, 2);
            setValueAt(rs.getString("RADIO_FREQ"), i, 3);
            setValueAt(rs.getString("CD"), i, 4);
            setValueAt(rs.getString("IPOD"),i, 5);
          }
          else if(systemDisplayed.equals("Cl")){ 
            //  "Cl"- Climate {"Date","Internal Temp", "External Temp", "Fan Speed", "Climate Status"}
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("INTERNAL_TEMP"), i, 1);
            setValueAt(rs.getString("EXTERNAL_TEMP"), i, 2);
            setValueAt(rs.getString("FAN_SPEED"), i, 3);
            setValueAt(rs.getString("CLIMATE_STATUS"), i, 4);
          }
          else if(systemDisplayed.equals("Co")){   
            //  "Co"- Communication {"Date","Phone Connected", "Total Call Time", "Communication Status"}
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("PHONE_CON"), i, 1);
            setValueAt(rs.getString("CALL_TIME"), i, 2);
            setValueAt(rs.getString("COM_STATUS"), i, 3);
          }
          else if(systemDisplayed.equals("LE")){
            // "LE"- Lighting & Electrical{"Date","Headlight Status", "Power Outlets Status", "Internal Light Status"}
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("HEADLIGHT_STATUS"), i, 1);
            setValueAt(rs.getString("POWER_OUTLETS"), i, 2);
            setValueAt(rs.getString("INTERNAL_LIGHTS"), i, 3);
          }
          else if(systemDisplayed.equals("TSB")){ 
            // "TSB"- Tires, Steering, Brakes("Date","Tire Pressure", "Suspension Status", "Brake Status", "Steering Status", "Brake Fluid Life")
            setValueAt(rs.getString("DATE_TIME"), i, 0);
            setValueAt(rs.getString("TIRE_PRESSURE"), i, 1);
            setValueAt(rs.getString("SUSP_STATUS"), i, 2);
            setValueAt(rs.getString("BRAKE_STATUS"), i, 3);
            setValueAt(rs.getString("STEERING_STATUS"), i, 4);
            setValueAt(rs.getString("BRAKE_FLUID_LIFE"), i, 5);
          }          
        }
      }
    }catch(SQLException e1){
      System.out.println("Table population of old record error: " + e1);
    }
  }
}
  